##################################################
#
# TABLES OF SOCIAL VICE (ALCOHOL AND TOBACCO) MODELS FOR
# OIL, ISLAM, and WOMEN PROJECT
#
# Paul Musgrave and Yu-Ming Liou
# musgrave@umass.edu and yl254@georgetown.edu
#
# Created 27 February 2016
#
##################################################

##################################################
# R Housekeeping
##################################################

# Remove and empty workspace
rm(list=ls())

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Packages
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

library(foreign)
library(sandwich)
library(apsrtable)
library(stargazer)
library(tonymisc)  # includes summaryR()
library(MASS) # for polr() which runs our ologit

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Functions
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 


# Change this to  your working directory
mywd <- "/Users/paulmusgrave/Dropbox/0001 Academic Projects/Ongoing/0127 Oil Islam Women/ISQ Accepted Submission/Replication"

setwd(paste(mywd,"/Code",sep=""))

source("statifyFN.R")
source("multipleOLSFN.R")
source("genModelsFN.R")
source("plotlineFN.R")
source("plotprepFN.R")
source("modelDefinitions.R")
source("tableOLSFN.R")
source("prepper.R")
source("helperFN.R")
tablenames <- read.csv("allmodels.csv", stringsAsFactors=FALSE)

p142.09.names <- prepper(p142.09)
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Data
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

setwd(paste(mywd,"/Data",sep=""))

data <- read.dta("NewDVModelsXSforISQFINAL2015.dta")

setwd("../Drafts/Tables")


#################################################
#
# Models of Oil Income and Vice Issues
# 
#################################################

# Female Smoking
tab.femalesmoking <- tableOLS(formulas=genModels(c("femsmoking"),
                                                    c(p142.09)),
                                 modelnames=c(names(p142.09)))

stargazer(tab.femalesmoking,
          title= "Models of Oil Income Per Capita and Female Tobacco Consumption",
          dep.var.labels = c("Tobacco Consumption"),
          covariate.labels=c(p142.09.names[,2],"Intercept"),
          out="ISQFINALAppendixTable20.tex",
          style="apsr",
          label="tab:femalesmoking",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Alcohol 1
tab.alcohol.1 <- tableOLS(formulas=genModels(c("totalalcohollitres"),
                                              c(p140.04)),
                           modelnames=c(LETTERS[1:7]))

stargazer(tab.alcohol.1,
          title= "Models of Oil Income Per Capita and Alcohol Consumption",
          dep.var.labels = c("Alcohol Consumption"),
          covariate.labels=c(p140.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable21.tex",
          style="apsr",
          label="tab:alcohol1",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Alcohol 2 -- split because of captions and size
tab.alcohol.2 <- tableOLS(formulas=genModels(c("totalalcohollitres"),
                                             c(p142.04)),
                          modelnames=c(LETTERS[1:7]))


stargazer(tab.alcohol.2,
          title= "Models of Oil Income Per Capita and Alcohol Consumption",
          dep.var.labels = c("Alcohol Consumption"),
          covariate.labels=c(p142.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable22.tex",
          style="apsr",
          label="tab:alcohol2",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Male Smoking
tab.malesmoking <- tableOLS(formulas=genModels(c("malesmoking"),
                                               c(p142.09)))

stargazer(tab.malesmoking,
          title= "Models of Oil Income Per Capita and Tobacco Consumption",
          dep.var.labels = c("Male Tobacco Consumption"),
          covariate.labels=c(p142.09.names[,2],"Intercept"),
          out="ISQFINALAppendixTable19.tex",
          style="apsr",
          label="tab:malesmoking",
          no.space=TRUE,
          omit.stat=c("ser","f"))
